package org.hxd.admin.Notices.controller;

import org.hxd.admin.Notices.service.AdminNoticeService;
import org.hxd.pojo.Notice;
import org.hxd.utils.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/admin/notice")
public class NoticeController {
    @Autowired
    AdminNoticeService adminNoticeService;

    @RequestMapping("/findNoticeAll")
    public String findNoticeAll(Notice notice, Model model, @RequestParam(defaultValue = "1") Integer pageIndex, String msg) {
        PageModel pageModel = new PageModel();
        pageModel.setPageIndex(pageIndex);
        pageModel.setPageSize(8);
        int count = adminNoticeService.findNoticeCount();
        pageModel.setRocordCount(count);
        if (msg!=null && msg.equals("1")) {
            model.addAttribute("msg", "添加成功！");
            pageModel.setPageIndex(pageModel.getTotalSize());
        }
        if (msg!=null && msg.equals("2")) {
            model.addAttribute("msg", "修改成功！");
        }
        List<Notice> noticeList = adminNoticeService.findNoticeAll(pageModel);
        model.addAttribute("pageModel", pageModel);
        model.addAttribute("notices", noticeList);
        return "admin/notices/list";
    }

    //添加公告
    @PostMapping("/addNoticeForm")
    public String addNoticeForm(Notice notice, Model model) {
        int rows = adminNoticeService.addNoticeForm(notice);
        if (rows > 0) {
            PageModel pageModel = new PageModel();
            pageModel.setPageSize(8);
            int noticeCount = adminNoticeService.findNoticeCount();
            pageModel.setRocordCount(noticeCount);
            return "redirect:/admin/notice/findNoticeAll?msg=" + "1";
        } else {
            return "admin/notices/error";
        }
    }

    //删除公告
    @RequestMapping("/deleteNotice")
    @ResponseBody
    public String deleteNotice(Integer id) {
        int rows = adminNoticeService.deleteNotice(id);
        if (rows > 0) {
            return "OK";
        } else {
            return "FAIL";
        }
    }

    //修改公告
    @RequestMapping("/findNoticeOne")
    public String findNoticeOne(Integer id, Model model) {
        Notice notice = adminNoticeService.findNoticeOne(id);
        model.addAttribute("n", notice);
        return "admin/notices/edit";
    }

    //修改
    @RequestMapping("/updateNotice")
    public String updateNotice(Notice notice) {
        int rows = adminNoticeService.updateNotice(notice);
        if (rows > 0) {
            return "redirect:/admin/notice/findNoticeAll?msg="+"2";
        } else {
            return "redirect:/admin/notice/findNoticeAll?msg="+"3";
        }
    }
}
